package com.xtkj.accountjava.controller;

import cn.hutool.db.sql.Condition;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xtkj.accountjava.base.Result;
import com.xtkj.accountjava.base.UnauthorizedException;
import com.xtkj.accountjava.entity.UserAccountEntity;
import com.xtkj.accountjava.entity.UserEntity;
import com.xtkj.accountjava.service.UserAccountService;
import com.xtkj.accountjava.service.UserService;
import com.xtkj.accountjava.utils.RSAUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping(value = "/account/list")
public class AccountController {

    @Autowired
    UserService userService;

    @Autowired
    UserAccountService userAccountService;

    @PostMapping("/add/account")
    public Result add_account(
            @RequestHeader(value = HttpHeaders.AUTHORIZATION, required = false) String token,
            @RequestParam Map<String,Object> requestMap
    ){
        UserEntity userEntity = userService.checkToken(token);
        if(userEntity==null){
            throw  new UnauthorizedException("登录过期");
        }
        String requestEntityData = (String) requestMap.get("data");
        UserAccountEntity userAccountEntity = RSAUtils.decryptStrToEntityData(requestEntityData, UserAccountEntity.class);
        if(userAccountEntity==null)return  Result.error("添加失败");
        UserAccountEntity addUserAccount = userAccountService.addUserAccount(userAccountEntity);
        if(addUserAccount==null)return  Result.error("添加失败");
        return  Result.ok(addUserAccount);
    }
    @PostMapping("/edit/account")
    public Result edit_account(
            @RequestHeader(value = HttpHeaders.AUTHORIZATION, required = false) String token,
            @RequestParam Map<String,Object> requestMap
    ){
        UserEntity userEntity = userService.checkToken(token);
        if(userEntity==null){
            throw  new UnauthorizedException("登录过期");
        }
        String requestEntityData = (String) requestMap.get("data");
        UserAccountEntity userAccountEntity = RSAUtils.decryptStrToEntityData(requestEntityData, UserAccountEntity.class);
        if(userAccountEntity==null)return  Result.error("修改失败");
        UserAccountEntity editUserAccount = userAccountService.updateUserAccount(userAccountEntity);


        //id	user_id	account_book_id	account_category_id	create_time	modify_time	user_acbook_id	acbook_type
//        int accountBookId = (int) parmsMap.get("accountBookId");
//        String description = (String) parmsMap.get("description");
//        UserAccountBookEntity userAccountBookEntity =new UserAccountBookEntity();
//        userAccountBookEntity.setUserId(Long.parseLong(userEntity.getId()+""));
//        userAccountBookEntity.setAccountBookId((long) accountBookId);
//        userAccountBookEntity.setCreateTime(new Date());
//        userAccountBookEntity.setDescription(description);
//        UserAccountBookEntity userAccountBook = userAccountService.addUserAccountBook(userAccountBookEntity);
        if(editUserAccount==null)return  Result.error("修改失败");
        return  Result.ok(editUserAccount);
    }

    @GetMapping(value = "/accounts")
    public Result getAccountList(
            @RequestHeader(value = HttpHeaders.AUTHORIZATION, required = false) String token,
            @RequestParam Map<String,Object> requestMap
    ){
        UserEntity userEntity = userService.checkToken(token);
        if(userEntity==null){
            throw  new UnauthorizedException("登录过期");
        }
        String requestEntityData = (String) requestMap.get("data");
        HashMap<String,Object> parmsMap = RSAUtils.decryptStrToEntityData(requestEntityData, HashMap.class);
        String startTime = (String) parmsMap.get("startTime");
        String endTime = (String) parmsMap.get("endTime");

        String userAcbookId = (String) parmsMap.get("userAcbookId");
        int currPage = (int) parmsMap.get("currPage");
        int pageSize = (int) parmsMap.get("pageSize");

        return  Result.ok(userAccountService.getAccountPage(
                startTime,endTime,
                userEntity.getId().intValue(),Integer.parseInt(userAcbookId),
                currPage,pageSize
        ));
    }
    @GetMapping(value = "/month/accounts")
    public Result getAccountMonthPage(
            @RequestHeader(value = HttpHeaders.AUTHORIZATION, required = false) String token,
            @RequestParam Map<String,Object> requestMap
    ){
        UserEntity userEntity = userService.checkToken(token);
        if(userEntity==null){
            throw  new UnauthorizedException("登录过期");
        }
        String requestEntityData = (String) requestMap.get("data");
        HashMap<String,Object> parmsMap = RSAUtils.decryptStrToEntityData(requestEntityData, HashMap.class);
        String startTime = (String) parmsMap.get("startTime");
        String endTime = (String) parmsMap.get("endTime");

        String userAcbookId = (String) parmsMap.get("userAcbookId");
//        int currPage = (int) parmsMap.get("currPage");
//        int pageSize = (int) parmsMap.get("pageSize");

        return  Result.ok(userAccountService.getAccountMonthPage(
                startTime,endTime,
                userEntity.getId().intValue(),Integer.parseInt(userAcbookId)
        ));
    }

    @GetMapping(value = "/week_month_year/accounts")
    public Result getWeekMonthYearAccounts(
            @RequestHeader(value = HttpHeaders.AUTHORIZATION, required = false) String token,
            @RequestParam Map<String,Object> requestMap
    ){
        UserEntity userEntity = userService.checkToken(token);
        if(userEntity==null){
            throw  new UnauthorizedException("登录过期");
        }
        String requestEntityData = (String) requestMap.get("data");
        HashMap<String,Object> parmsMap = RSAUtils.decryptStrToEntityData(requestEntityData, HashMap.class);
        String startWeekTime = (String) parmsMap.get("startWeekTime");
        String endWeekTime = (String) parmsMap.get("endWeekTime");

        String startMonthTime = (String) parmsMap.get("startMonthTime");
        String endMonthTime = (String) parmsMap.get("endMonthTime");

        String startYearTime = (String) parmsMap.get("startYearTime");
        String endYearTime = (String) parmsMap.get("endYearTime");


                String userAcbookId = (String) parmsMap.get("userAcbookId");
//        int currPage = (int) parmsMap.get("currPage");
//        int pageSize = (int) parmsMap.get("pageSize");

        return  Result.ok(userAccountService.getWeekMonthYearAccounts(
                startWeekTime,endWeekTime,
                startMonthTime,endMonthTime,
                startYearTime,endYearTime,
                userEntity.getId().intValue(),Integer.parseInt(userAcbookId)
        ));
    }

    @GetMapping(value = "/month/trend/accounts")
    public Result getMonthTrendAccounts(
            @RequestHeader(value = HttpHeaders.AUTHORIZATION, required = false) String token,
            @RequestParam Map<String,Object> requestMap
    ){
        UserEntity userEntity = userService.checkToken(token);
        if(userEntity==null){
            throw  new UnauthorizedException("登录过期");
        }
        String requestEntityData = (String) requestMap.get("data");
        HashMap<String,Object> parmsMap = RSAUtils.decryptStrToEntityData(requestEntityData, HashMap.class);
        String startMonthTime = (String) parmsMap.get("startMonthTime");
        String endMonthTime = (String) parmsMap.get("endMonthTime");
        String userAcbookId = (String) parmsMap.get("userAcbookId");


        return  Result.ok(userAccountService.getMonthTrendAccounts(
                startMonthTime,endMonthTime,
                userEntity.getId().intValue(),Integer.parseInt(userAcbookId)
        ));
    }

}
